﻿using UnityEngine;
using System.Collections;

public class CharacterSelect : MonoBehaviour {
	
	public UILabel inputArea;
	public UISprite characterPoseSprite;
	public UISprite characterInfoSprite;
	
	string selectedSprite;
	
	public UISprite darkMask;
	bool readyToLoad = false;
	
	public void Start()
	{
		//PlayerPrefs.DeleteAll();
		inputArea.text = DataScript.GetPlayerName();
		Load();
	}
	
	void Update()
	{
		if(readyToLoad)
		{
			Color tmpCol = darkMask.color;
			tmpCol.a += Time.deltaTime * 0.6f;
			darkMask.color = tmpCol;
			if(tmpCol.a >= 1.0f)
			{
					Application.LoadLevel("LobbyScene");
			}
		}
		else
		{ 
			Color tmpCol = darkMask.color;
			tmpCol.a -= Time.deltaTime * 0.6f;
			darkMask.color = tmpCol;
			if(tmpCol.a <= 0f)
			{
				darkMask.collider.enabled = false;
				//if(Input.anyKeyDown)
				//{
					//Debug.Log("TRUE");
				//	readyToLoad = true;
				//	tmpCol.a = 0;
				//	darkMask.color = tmpCol;
				//}
			}
		}
	}
	
	public void SelectButton()
	{
		//Baduer
		if(selectedSprite == "btn05_02")
		{
			DataScript.SetCharacter(CHARACTERS.BADUER);
			DataScript.SetCharacterAsBought((int)CHARACTERS.BADUER);
		}
		
		//Mabs
		else if(selectedSprite == "btn05_03")
		{
			DataScript.SetCharacter(CHARACTERS.MABS);
			DataScript.SetCharacterAsBought((int)CHARACTERS.MABS);
		}
		
		DataScript.SetGameState(GAMESTATE.ISFIRSTLOGIN);
		DataScript.SetPlayerName(inputArea.text);
		
		Color tmpCol = darkMask.color;
		readyToLoad = true;
		tmpCol.a = 0;
		darkMask.color = tmpCol;
		darkMask.collider.enabled = true;
		
		Load();
	}
	
	void Load()
	{
		//if(DataScript.IsGameState(GAMESTATE.ISFIRSTLOGIN) != 0)
		//	Application.LoadLevel("LobbyScene");
	}
	
	public void CharacterSelection()
	{
		UISprite targetSprite = UIButton.current.tweenTarget.GetComponent<UISprite>();
		
		//Baduer
		if(targetSprite.spriteName == "btn05_02")
		{
			characterInfoSprite.spriteName = "plate_charsel01";
			characterPoseSprite.spriteName = "charposes_02";
		}
		
		//Mabs
		else if(targetSprite.spriteName == "btn05_03")
		{
			characterInfoSprite.spriteName = "plate_charsel02";
			characterPoseSprite.spriteName = "charposes_03";
		}
		
		selectedSprite = targetSprite.spriteName;
	}
}
